package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.dto.CourseTeacherDTO;
import com.xuecheng.api.content.model.vo.CourseTeacherVO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.convert.CourseTeacherConvert;
import com.xuecheng.content.entity.CourseTeacher;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {
    @Autowired
    private CourseBaseService courseBaseService;

    @Override
    public List<CourseTeacherDTO> queryTeacherById(Long courseBaseId, Long companyId) {
//判断关键数据
        if (ObjectUtils.isEmpty(courseBaseId)||ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        LambdaUpdateWrapper<CourseTeacher> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(CourseTeacher::getCourseId, courseBaseId);
        List<CourseTeacher> list = this.list(lambdaUpdateWrapper);
        //判断是否有教师
        if (ObjectUtils.isEmpty(list)) {
            return null;
        }
        ArrayList<CourseTeacherDTO> courseTeacherDTOS = new ArrayList<>();
        for (CourseTeacher courseTeacher : list) {

            CourseTeacherDTO teacherDTO = CourseTeacherConvert.INSTANCE.PO_DTO(courseTeacher);
            courseTeacherDTOS.add(teacherDTO);
        }


        return courseTeacherDTOS;
    }

    @Override
    @Transactional
    public CourseTeacherDTO AddOrModifyTeacher(CourseTeacherDTO courseTeacherDTO, Long companyId) {
     // 统一处理用户不知道缺少那一个数据  但美观
        if (ObjectUtils.isEmpty(courseTeacherDTO.getCourseId())) {
            ExceptionCast.cast(ContentErrorCode.E_120090);
        }
        if (ObjectUtils.isEmpty(courseTeacherDTO.getPhotograph())) {
            ExceptionCast.cast(ContentErrorCode.E_120091);
        }
        if (ObjectUtils.isEmpty(courseTeacherDTO.getPosition())) {
            ExceptionCast.cast(ContentErrorCode.E_120092);
        }
        if (ObjectUtils.isEmpty(courseTeacherDTO.getTeacherName())) {
            ExceptionCast.cast(ContentErrorCode.E_120093);
        }
        CourseTeacher courseTeacher = CourseTeacherConvert.INSTANCE.dto2po(courseTeacherDTO);
        //若要新增需判断id是否正常
        //正常则添加修改数据时间
        if (ObjectUtils.isEmpty(courseTeacherDTO.getCourseTeacherId())) {
            courseTeacher.setCreateDate(LocalDateTime.now());
            boolean rusult = this.save(courseTeacher);
            if (!rusult) {
                ExceptionCast.cast(ContentErrorCode.E_120094);
            }

        }else {
            LambdaUpdateWrapper<CourseTeacher> updateWrapper = new LambdaUpdateWrapper<>();
//                updateWrapper.setEntity(courseTeacher);
            updateWrapper.set(CourseTeacher::getTeacherName, courseTeacher.getTeacherName());
            updateWrapper.set(CourseTeacher::getPhotograph, courseTeacher.getPhotograph());
            updateWrapper.set(CourseTeacher::getPosition, courseTeacher.getPosition());
            updateWrapper.set(CourseTeacher::getIntroduction, courseTeacher.getIntroduction());
            updateWrapper.eq(CourseTeacher::getId, courseTeacher.getId());

           boolean result = this.update(updateWrapper);
            if (!result) {
                //失败抛出教师信息操作失败
                ExceptionCast.cast(ContentErrorCode.E_1200501);
            }
        }
        CourseTeacher teacherId = this.getById(courseTeacher.getId());
        if (ObjectUtils.isEmpty(teacherId)) {
            ExceptionCast.cast(ContentErrorCode.E_1200503);
        }
        //数据转换
        CourseTeacherDTO teacherDTO = CourseTeacherConvert.INSTANCE.entity2dto(teacherId);
        return teacherDTO;
    }

    @Override
    @Transactional
    public boolean deleteTeacherById(Long courseId, Long courseTeacherId, Long companyId) {
//        System.out.println(courseId);
//        System.out.println(courseTeacherId);
        //判断关键数据
        if (ObjectUtils.isEmpty(courseId) ||
                ObjectUtils.isEmpty(courseTeacherId) ||
                ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        boolean remove = this.removeById(courseTeacherId);

        if (!remove) {
            ExceptionCast.cast(ContentErrorCode.E_120095);
        }
        return true;
    }


}
